package com.gjs.bedorder.control;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.gjs.bedorder.business.BedInfo;

/**
 * Servlet implementation class Order
 */
@WebServlet("/order")
public class Order extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Order() {
        super();
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String bedNo = request.getParameter("bed_no");
		String userId = (String) request.getSession().getAttribute("userId");
		String name = (String) request.getSession().getAttribute("name");
		String msg = "";
		if (userId == null) {
			msg = "0";
		} else if (BedInfo.checkOrder(bedNo)) {
			msg = "1";
		} else if (BedInfo.checkOrder(userId)) {
			msg = "2";
		} else {
			BedInfo.orderBed(bedNo, userId, name);
			msg = "3";
		}
		HttpSession session = request.getSession();
		session.setAttribute("msg", msg);
		session.setAttribute("count", 1);
	}

}
